package com.maotouying.owlsmart.SearchPage;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.maotouying.owlsmart.R;
import com.maotouying.owlsmart.SmartLayout.PageMenu.IndicatorView;
import com.maotouying.owlsmart.SmartLayout.PageMenu.PageMenuLayout;
import com.maotouying.owlsmart.SmartLayout.PageMenu.ScreenUtil;
import com.maotouying.owlsmart.SmartLayout.PageMenu.holder.AbstractHolder;
import com.maotouying.owlsmart.SmartLayout.PageMenu.holder.PageMenuViewHolderCreator;
import com.maotouying.owlsmart.SmartUtil.SystemUtil;
import com.maotouying.owlsmart.homePage.ModelHomeEntrance;
import com.maotouying.owlsmart.homePage.homeActivity;
import com.maotouying.owlsmart.homePage.younglady_C;
import com.maotouying.owlsmart.magnetPage.magnetActivity;
import com.maotouying.owlsmart.moviePage.movieListActivity;
import com.maotouying.owlsmart.tvPage.tvListActivity;
import com.maotouying.owlsmart.youngLadyPage.youngladyListActivity;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity {


    List<ModelHomeEntrance> homeEntrances;
    IndicatorView entranceIndicatorView;
    PageMenuLayout<ModelHomeEntrance> mPageMenuLayout;
    private ImageView iv_search;
    private EditText et_search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        //顶部框颜色
        SystemUtil.setStatusBarColor(this, this.getResources().getColor(R.color.top_background_color));
        //屏幕适配绑定，一定要写，否则加载不出来
        ScreenUtil.init(SearchActivity.this);
        //加载可滑动的导航栏
        initView();
        initData();
        init();
    }


    private void initView() {
        entranceIndicatorView = (IndicatorView)findViewById(R.id.main_home_entrance_indicator);
        mPageMenuLayout = (PageMenuLayout)findViewById(R.id.pagemenu);


        et_search=  (EditText) findViewById(R.id.et_search);
        et_search.setOnEditorActionListener(new SearchActivity.mEditorActionListener());
        //搜索按钮点击事件
        iv_search =  (ImageView) findViewById(R.id.iv_search);
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = "";
                if(et_search.getText().toString().isEmpty()){
                    content = SearchActivity.this.getString(R.string.app_net);
                }else {
                    content=et_search.getText().toString();
                }
                Toast.makeText(SearchActivity.this, content, Toast.LENGTH_SHORT).show();
            }
        });
    }


    private class mEditorActionListener implements TextView.OnEditorActionListener {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            switch (v.getId()) {
                case R.id.et_search://搜索
                    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                        et_search.setText("");
                    }
                    break;
            }
            return false;
        }
    }


    //初始化数据
    private void initData() {
        homeEntrances = new ArrayList<>();
        //第1页显示
        //  homeEntrances.add(new ModelHomeEntrance("公告", R.drawable.ic_megaphone));
        homeEntrances.add(new ModelHomeEntrance("电影", R.drawable.ic_movie));
        homeEntrances.add(new ModelHomeEntrance("讨论", R.drawable.ic_speech));
        homeEntrances.add(new ModelHomeEntrance("磁力", R.drawable.ic_magnet));
        //  homeEntrances.add(new ModelHomeEntrance("我的", R.drawable.ic_hat));
        homeEntrances.add(new ModelHomeEntrance("品茶", R.drawable.ic_cocktails));

        //第2页显示
        //  homeEntrances.add(new ModelHomeEntrance("地图", R.drawable.ic_map));

        homeEntrances.add(new ModelHomeEntrance("视频", R.drawable.ic_tv));
        homeEntrances.add(new ModelHomeEntrance("小说", R.drawable.ic_book));
        homeEntrances.add(new ModelHomeEntrance("文件", R.drawable.ic_folder));
        homeEntrances.add(new ModelHomeEntrance("设置", R.drawable.ic_setting));
        //  homeEntrances.add(new ModelHomeEntrance("升级", R.drawable.ic_rocket));
    }
    private void init() {
        mPageMenuLayout.setPageDatas(homeEntrances, new PageMenuViewHolderCreator() {
            @Override
            public AbstractHolder createHolder(View itemView) {
                return new AbstractHolder<ModelHomeEntrance>(itemView) {
                    private TextView entranceNameTextView;
                    private ImageView entranceIconImageView;

                    @Override
                    protected void initView(View itemView) {
                        entranceIconImageView = itemView.findViewById(R.id.entrance_image);
                        entranceNameTextView = itemView.findViewById(R.id.entrance_name);
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) ((float) ScreenUtil.getScreenWidth() / 4.0f));
                        itemView.setLayoutParams(layoutParams);
                    }

                    @Override
                    public void bindView(RecyclerView.ViewHolder holder, final ModelHomeEntrance data, int pos) {
                        entranceNameTextView.setText(data.getName());
                        entranceIconImageView.setImageResource(data.getImage());
                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //Toast.makeText(getActivity(), data.getName(), Toast.LENGTH_SHORT).show();
                                String clickName =   data.getName();
                                if(clickName.equals("电影")){
                                    //获取edittext的数据
                                    String et = et_search.getText().toString();
                                    Intent intent=new Intent();
                                    intent.putExtra("search", et);//设置参数,""
                                    intent.setClass(SearchActivity.this,  movieListActivity.class);//从哪里跳到哪里
                                    SearchActivity.this.startActivity(intent);
                                }else if(clickName.equals("聊天")){
//                                    Intent intent = new Intent(homeActivity.this, deviceTypeActivity.class);
//                                    startActivity(intent);
                                }else if(clickName.equals("磁力")){

                                    //获取edittext的数据
                                    String et = et_search.getText().toString();
                                    if(!et.isEmpty()){

                                        Intent intent=new Intent();
                                        intent.putExtra("search", et);//设置参数,""
                                        intent.setClass(SearchActivity.this,  magnetActivity.class);//从哪里跳到哪里
                                        SearchActivity.this.startActivity(intent);
                                    }else{
                                        Toast.makeText(SearchActivity.this, "搜索栏输入为空", Toast.LENGTH_SHORT).show();
                                    }


                                }else if(clickName.equals("我的")){

                                }else if(clickName.equals("文件")){

                                }
                                else if(clickName.equals("设置")){
//                                    Intent intent = new Intent(homeActivity.this, settingActivity.class);
//                                    startActivity(intent);
                                }else if(clickName.equals("品茶")) {

                                    String et = et_search.getText().toString();
                                    Intent intent=new Intent();
                                    intent.putExtra("search", et);//设置参数,""
                                    intent.setClass(SearchActivity.this,  youngladyListActivity.class);//从哪里跳到哪里
                                    SearchActivity.this.startActivity(intent);


                                }
                                else if(clickName.equals("视频")){
                                    String et = et_search.getText().toString();
                                    Intent intent=new Intent();
                                    intent.putExtra("search", et);//设置参数,""
                                    intent.setClass(SearchActivity.this,  tvListActivity.class);//从哪里跳到哪里
                                    SearchActivity.this.startActivity(intent);
                                }

                            }
                        });
                    }
                };
            }

            @Override
            public int getLayoutId() {
                return R.layout.item_home_entrance;
            }
        });
        entranceIndicatorView.setIndicatorCount(mPageMenuLayout.getPageCount());
        mPageMenuLayout.setOnPageListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                entranceIndicatorView.setCurrentIndicator(position);
            }
        });
    }



}